package com.yuki312.backoff.example;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import org.junit.Test;

import com.yuki312.backoff.core.BackoffAlgorithm;
import com.yuki312.backoff.ktx.BackoffExt;
import com.yuki312.backoff.rxjava2.RxBackoff;

import static org.junit.Assert.assertEquals;

public class ExampleOhosTest {
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.yuki312.backoff.example", actualBundleName);
    }

    @Test
    public void getRxBackoffMaxRetryCount(){
        new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
            BackoffAlgorithm algorithm = BackoffExt.binaryExponentialBackoff();
            int retry = 5;
            Observable.error(new Exception("sample"))
                    .doOnError(new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                        }
                    })
                    .retryWhen(RxBackoff.of(algorithm, retry).observable())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object object) {
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    });

            assertEquals(5,RxBackoff.getMaxRetry());
        });
    }

    @Test
    public void getRxBackoffAlgorithm(){
        new EventHandler(EventRunner.getMainEventRunner()).postTask(() -> {
            BackoffAlgorithm algorithm = BackoffExt.binaryExponentialBackoff();
            Observable.error(new Exception("sample"))
                    .doOnError(new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                        }
                    })
                    .retryWhen(RxBackoff.of(algorithm, 5).observable())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object object) {
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    });
            assertEquals(BackoffExt.binaryExponentialBackoff(), RxBackoff.getAlgorithm());
        });
    }
}